﻿//===============================================================================
// PostSharp Poco implementation for EntityFramework
//===============================================================================
// Copyright © Ruurd Boeke.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
using System;
using System.Reflection;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data.Objects.DataClasses;
using System.ComponentModel;
using System.Runtime.Serialization;
using EditableBusinessObjects.Postsharp.Public;
using System.IO;
using System.Xml;
using CyclicReferences.Public;
using EntityFrameworkContrib.PostSharp4EF;

namespace EFContrib.PS4EF.Tst.OneSimpleType
{
    /// <summary>
    /// Happy flow: 
    /// all the work has been done, we can only check if it has been done correctly
    /// the Person class is named correctly (Person) and it's namespace (EntityFrameworkContrib.PostSharp4EF.Tests.OneSimpleType)
    /// is the same as the namespace as defined in the edmx file
    /// that means, the simplest possible usage of the Ipoco attribute is possible
    /// </summary>
    [TestClass]
    public class HappyFlowTest
    {
        #region setup
        public HappyFlowTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion 
        #endregion

        [TestMethod]
        public void testTypeAttributesTest()
        {
            Type t = typeof(Person);

            EdmEntityTypeAttribute typeAttr =
                t.GetCustomAttributes(typeof(EdmEntityTypeAttribute), false)[0] as EdmEntityTypeAttribute;

            Assert.IsTrue(typeAttr.Name.Equals(t.Name));
            Assert.IsTrue(typeAttr.NamespaceName.Equals(t.Namespace));
        }

        [TestMethod]
        public void testPropertyAttributesTest()
        {
            Type t = typeof(Person);

            EdmScalarPropertyAttribute idProperty = t.GetProperty("PersonID").GetCustomAttributes(typeof(EdmScalarPropertyAttribute),
                false)[0] as EdmScalarPropertyAttribute;

            Assert.IsTrue(idProperty.EntityKeyProperty == true);
            Assert.IsTrue(idProperty.IsNullable == false);

            EdmScalarPropertyAttribute firstnameProperty = t.GetProperty("Firstname").GetCustomAttributes(typeof(EdmScalarPropertyAttribute),
                false)[0] as EdmScalarPropertyAttribute;

            Assert.IsTrue(firstnameProperty.EntityKeyProperty == false);
            Assert.IsTrue(firstnameProperty.IsNullable == true);

            EdmScalarPropertyAttribute lastnameProperty = t.GetProperty("Lastname").GetCustomAttributes(typeof(EdmScalarPropertyAttribute),
                false)[0] as EdmScalarPropertyAttribute;

            Assert.IsTrue(lastnameProperty.EntityKeyProperty == false);
            Assert.IsTrue(lastnameProperty.IsNullable == false);
        }



        [TestMethod]
        public void testIPocoInterfacesImplemented()
        {
            Type t = typeof(Person);
            Assert.IsNotNull(t.GetInterface("IEntityWithChangeTracker"));
            Assert.IsNotNull(t.GetInterface("IEntityWithKey"));
            Assert.IsNotNull(t.GetInterface("IEntityWithRelationships"));
        }

    }







}
